﻿using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.User;
using CE.Global;
using CE.Provider;
using CE.Database;
using CE.Security;
using CE.UserAccess;

public partial class admin_user_subpage_access : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/Admin/AdminSecureLogin.aspx");
        }
        if (!IsPostBack)
        {
            DataTable _dtCompany = General.GetComboItem("TH.ProviderCompany", false);
            ddlProviderCompany.DataSource = _dtCompany;
            ddlProviderCompany.DataTextField = _dtCompany.Columns[0].ColumnName.ToString();
            ddlProviderCompany.DataValueField = _dtCompany.Columns[1].ColumnName.ToString();
            ddlProviderCompany.DataBind();

            ddlProviderCompany.Items.Insert(0, new ListItem("Select", "-1"));

            btnSave.Visible = false;
        }
    }

    protected void ddlProviderCompany_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadUserList();
        LoadMainPageList();
        grvUserAccess.DataBind();
        if (grvUserAccess.Rows.Count > 0)
        {
            btnSave.Visible = true;
        }
        else
        {
            btnSave.Visible = false;
        }
    }

    protected void ddlMainPage_SelectedIndexChanged(object sender, EventArgs e)
    {
        grvUserAccess.DataBind();
        if (grvUserAccess.Rows.Count > 0)
        {
            btnSave.Visible = true;
        }
        else
        {
            btnSave.Visible = false;
        }
    }

    protected void ddlUser_SelectedIndexChanged(object sender, EventArgs e)
    {
        grvUserAccess.DataBind();
        if (grvUserAccess.Rows.Count > 0)
        {
            btnSave.Visible = true;
        }
        else
        {
            btnSave.Visible = false;
        }
    }

    protected void grvUserAccess_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Int64 _MainPageId = Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "MainPageId"));
            Int64 _SubPageId = Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "SubPageId"));
            Int64 _UserId = Convert.ToInt64(ddlUser.Text);
            Int64 _providerCompanyId = Convert.ToInt64(ddlProviderCompany.Text);

            DropDownList ddlUserAccess = (DropDownList)e.Row.FindControl("ddlUserAccess");
            string _CXSubPageName = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "CXSubPageName"));

            ddlUserAccess.Items.Clear();
            if (_CXSubPageName == "CXDPFCI" || _CXSubPageName == "CXDPFTI" || _CXSubPageName == "CXDPFPA")
            {
                ddlUserAccess.Items.Insert(0, new ListItem("View/Download", "400"));
                ddlUserAccess.Items.Insert(0, new ListItem("View", "100"));
                ddlUserAccess.Items.Insert(0, new ListItem("Not Accessible", "0"));
            }
            else if (_CXSubPageName == "CXCIRVS" || _CXSubPageName == "CXCIRSS")
            {
                ddlUserAccess.Items.Insert(0, new ListItem("View", "100"));
                ddlUserAccess.Items.Insert(0, new ListItem("Not Accessible", "0"));
            }
            else
            {
                ddlUserAccess.Items.Insert(0, new ListItem("View/Update/Delete", "300"));
                ddlUserAccess.Items.Insert(0, new ListItem("View/Update", "200"));
                ddlUserAccess.Items.Insert(0, new ListItem("View", "100"));
                ddlUserAccess.Items.Insert(0, new ListItem("Not Accessible", "0"));
            }

            MyDatabase _db = new MyDatabase();
            DataTable _dtResult = new DataTable();
            SortedList _sPara = new SortedList();
            _sPara.Add("@ProviderCompanyId", _providerCompanyId);
            _sPara.Add("@MainPageId", _MainPageId);
            _sPara.Add("@SubPageId", _SubPageId);
            _sPara.Add("@RefCEUserId", _UserId);
            _dtResult = _db.GetDataTableBySP("sprocProviderUserAccessSubPagesSelectItemListByUser", _sPara);
            if (_dtResult != null && _dtResult.Rows.Count > 0)
            {
                ddlUserAccess.Text = _dtResult.Rows[0]["AccessLevel"].ToString();
            }
            else
            {
                ddlUserAccess.SelectedIndex = 0;
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (ddlUser.Text != "0" && ddlMainPage.Text != "0")
        {
            SubPageUserAccess _subpageuseraccess = new SubPageUserAccess();
            SubPageUserAccessDB _subpageuseraccessdb = new SubPageUserAccessDB();
            int _count = grvUserAccess.Rows.Count;
            foreach (GridViewRow tt in grvUserAccess.Rows)
            {
                if (tt.RowType == DataControlRowType.DataRow)
                {
                    Int32 _AccessLevel = 0;
                    Label lblMainPageId = (Label)tt.FindControl("lblMainPageId");
                    Label lblSubPageId = (Label)tt.FindControl("lblSubPageId");
                    DropDownList ddlUserAccess = (DropDownList)tt.FindControl("ddlUserAccess");
                    _AccessLevel = Convert.ToInt32(ddlUserAccess.Text);

                    _subpageuseraccess.RefCEUserId = Convert.ToInt64(ddlUser.Text);
                    _subpageuseraccess.ProviderCompanyId = Convert.ToInt64(ddlProviderCompany.Text);
                    _subpageuseraccess.MainPageId = Convert.ToInt64(lblMainPageId.Text.ToString());
                    _subpageuseraccess.SubPageId = Convert.ToInt64(lblSubPageId.Text.ToString());
                    _subpageuseraccess.AccessLevel = _AccessLevel;
                    _subpageuseraccess.Status = true;
                    _subpageuseraccessdb.Insert(_subpageuseraccess);
                }
            }
        }
    }

    private void LoadUserList()
    {
        Int64 _ProviderCompanyId = Convert.ToInt64(ddlProviderCompany.Text);
        MyDatabase _db = new MyDatabase();
        DataTable _dtUser = new DataTable();
        SortedList _sPara = new SortedList();
        _sPara.Add("@ProviderCompanyId", _ProviderCompanyId);
        _dtUser = _db.GetDataTableBySP("sprocProviderUserSelectItemList", _sPara);

        if (_dtUser != null && _dtUser.Rows.Count > 0)
        {
            ddlUser.Items.Clear();
            for (int i = 0; i < _dtUser.Rows.Count; i++)
            {
                ddlUser.Items.Add(new ListItem(_dtUser.Rows[i]["Email"].ToString(), _dtUser.Rows[i]["CEID"].ToString()));
            }
        }
        else
        {
            ddlUser.Items.Clear();
            ddlUser.Items.Insert(0, new ListItem("None", "0"));
        }
    }

    private void LoadMainPageList()
    {
        Int64 _ProviderCompanyId = Convert.ToInt64(ddlProviderCompany.Text);
        MyDatabase _db = new MyDatabase();
        DataTable _dtMainPage = new DataTable();
        SortedList _sPara = new SortedList();
        _sPara.Add("@ProviderCompanyId", _ProviderCompanyId);
        _dtMainPage = _db.GetDataTableBySP("sprocProviderMainPagesSelectItemList", _sPara);

        if (_dtMainPage != null && _dtMainPage.Rows.Count > 0)
        {
            ddlMainPage.Items.Clear();
            for (int i = 0; i < _dtMainPage.Rows.Count; i++)
            {
                ddlMainPage.Items.Add(new ListItem(_dtMainPage.Rows[i]["ProviderPageName"].ToString(), _dtMainPage.Rows[i]["MainPageId"].ToString()));
            }
        }
        else
        {
            ddlMainPage.Items.Clear();
            ddlMainPage.Items.Insert(0, new ListItem("None", "0"));
        }
    }

}
